/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.aop.annotation.BisLog;
import me.zhengjie.aop.service.BisLogService;
import me.zhengjie.business.domain.*;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.domain.sup.Rygl;
import me.zhengjie.business.service.*;
import me.zhengjie.business.service.dto.*;
import me.zhengjie.config.DebugProperties;
import me.zhengjie.inter.service.MpSnryService;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.DefaultUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-15
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "居家照护申请管理")
@RequestMapping("/api/zhsq")
public class ZhsqController {

    private final ZhsqService zhsqService;
    private final SbgsService sbgsService;
    private final HljgService hljgService;

    private final RyglService ryglService;
    private final BisLogService bisLogService;

    private final GsglService gsglService;
    private final PgfyProService pgfyProService;
    private final ZhsqProService zhsqProService;
    private final MpSnryService mpSnryService;


    private final ZhsqGlService zhsqGlService;
    private final ZhsqSnService zhsqSnService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('zhsq:list')")
    public void exportZhsq(HttpServletResponse response, ZhsqQueryCriteria criteria) throws IOException {
        zhsqService.download(zhsqService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询机构申请中的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> queryZhsq(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("00");
        list.add("01");
        list.add("02");
        list.add("03");
        list.add("04");
        list.add("54");
        list.add("55");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryZhsqGl")
    @ApiOperation("查询机构申请中的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> queryZhsqGl(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("00");
        list.add("01");
        list.add("02");
        list.add("03");
        list.add("04");
        list.add("54");
        list.add("55");
        list.add("80");
        criteria.setBisZhsqZt(list);

        List<String> listGl = new ArrayList<>();
        listGl.add("85周岁以上");
        listGl.add("90周岁以上");
        criteria.setBisZhsqSndj(listGl);


        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryZhsqSn")
    @ApiOperation("查询机构申请中的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> queryZhsqSn(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("00");
        list.add("01");
        list.add("02");
        list.add("03");
        list.add("04");
        list.add("11");
        list.add("22");
        list.add("54");
        list.add("55");
        list.add("80");
        criteria.setBisZhsqZt(list);

        List<String> listGl = new ArrayList<>();
        listGl.add("中度失能");
        listGl.add("重度失能");
        listGl.add("重度失能Ⅰ级");
        listGl.add("重度失能Ⅱ级");
        listGl.add("重度失能Ⅲ级");
        criteria.setBisZhsqSndj(listGl);


        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zhsqJgFh")
    @ApiOperation("查询机构复核的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> zhsqJgFh(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("05");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zhsqJgYgq")
    @ApiOperation("查询机构即将过期的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> zhsqJgYgq(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("05");
        criteria.setBisZhsqZt(list);
        criteria.setBisZhsqDyjsrq2(DateTimeUtil.addDays(+5));
        criteria.setBisZhsqDyjsrq1(DateTimeUtil.getNow());

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zhsqJgTh")
    @ApiOperation("查询机构退回的照护")
    @PreAuthorize("@el.check('zhsq:list')")
        public ResponseEntity<Object> zhsqJgTh(ZhsqQueryCriteria criteria, Pageable pageable) {

            Long userid = SecurityUtils.getCurrentUserId();
            Gsgl gsgl = gsglService.queryGsgl(userid);
            Hljg hljg = new Hljg();
            hljg.setBisGsglId(gsgl.getBisGsglId());

            criteria.setHljg(hljg);
            criteria.setBisZhsqZhlb("01");

            List<String> list = new ArrayList<>();
            list.add("80");
            criteria.setBisZhsqZt(list);

            return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
        }

    @GetMapping(value = "/zhsqJgBh")
    @ApiOperation("查询机构驳回的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> zhsqJgBh(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("-4");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zhsqJgGq")
    @ApiOperation("查询机构过期的照护")
    @PreAuthorize("@el.check('zhsq:list')")
    public ResponseEntity<Object> zhsqJgGq(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        criteria.setBisZhsqZhlb("01");

        List<String> list = new ArrayList<>();
        list.add("90");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/approve")
    @ApiOperation("查询已提交的机构照护")
    public ResponseEntity<Object> queryApprove(ZhsqQueryCriteria criteria, Pageable pageable){

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);


        String hljgId = criteria.getBisHljgId();

        // 清空
        criteria.setBisHljgId(null);

        List<String> list = new ArrayList<>();
        list.add("01");
        criteria.setBisZhsqZt(list);

        HljgDto hljg = hljgService.findById(hljgId);
        criteria.setBisZhsqHljgBm(hljg.getBisGsglYljgdm());

        String pggsId = gsgl.getBisGsglId();
/*        String sbgsId = hljg.getSbgs().getBisGsglId();

        // 判断护理机构是否存在评估公司
        if(StringUtils.isEmpty(pggsId)) {
            criteria.setBisPggsId(sbgsId);
        } else {
        }*/
        criteria.setBisPggsId(pggsId);
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/assigned")
    @ApiOperation("查询已分配的机构照护")
    public ResponseEntity<Object> queryAssigned(ZhsqQueryCriteria criteria, Pageable pageable){
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        String pggsId = gsgl.getBisGsglId();
        String hljgId = criteria.getBisHljgId();
        // 清空
        criteria.setBisHljgId(null);
        HljgDto hljg = hljgService.findById(hljgId);
        criteria.setBisZhsqHljgBm(hljg.getBisGsglYljgdm());
        criteria.setBisPggsId(pggsId);

        List<String> list = new ArrayList<String>();
        list.add("02");
        list.add("03");
        criteria.setBisZhsqZt(list);
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/assessHome")
    @ApiOperation("查询已评估的居家照护")
    public ResponseEntity<Object> queryAssessHome(ZhsqQueryCriteria criteria, Pageable pageable){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisSbgsId(gsgl.getBisGsglId());
        List<String> list = new ArrayList<>();
        list.add("04");
        criteria.setBisZhsqZt(list);
        criteria.setBisZhsqZhlb("03");
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/assessOrg")
    @ApiOperation("查询已评估的机构照护")
    public ResponseEntity<Object> queryAssessOrg(ZhsqQueryCriteria criteria, Pageable pageable){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisSbgsId(gsgl.getBisGsglId());
        List<String> list = new ArrayList<>();
        list.add("04");
        criteria.setBisZhsqZt(list);
        criteria.setBisZhsqZhlb("01");
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @GetMapping(value = "/assessJbzx")
    @ApiOperation("经办中心查询待复核的照护")
    public ResponseEntity<Object> assessJbzx(ZhsqQueryCriteria criteria, Pageable pageable){
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        //    criteria.setBisSbgsId(gsgl.getBisGsglId());
        } else if(roleString.indexOf("管理员") != -1) {
        //    criteria.setBisZhsqSnryYbssqx(String.valueOf(deptJson.get("id")));
        }
        List<String> list = new ArrayList<>();
        list.add("54");
        criteria.setBisZhsqZt(list);
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zpspList")
    @ApiOperation("查询居家自评审批")
    public ResponseEntity<Object> zpspList(ZhsqQueryCriteria criteria, Pageable pageable){

        List<String> list = new ArrayList<>();
        list.add(DefaultUtil.ZHZT_DSP);
        criteria.setBisZhsqZt(list);
        criteria.setBisZhsqZhlb("03");

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setBisPggsId(gsgl.getBisGsglId());

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zpspOrgList")
    @ApiOperation("查询机构自评审批")
    public ResponseEntity<Object> zpspOrgList(ZhsqQueryCriteria criteria, Pageable pageable){

        List<String> list = new ArrayList<>();
        list.add(DefaultUtil.ZHZT_DSP);
        criteria.setBisZhsqZt(list);
        criteria.setBisZhsqZhlb("01");

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setBisPggsId(gsgl.getBisGsglId());

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("导出自评审批")
    @ApiOperation("导出自评审批")
    @GetMapping(value = "/zpspList/download")
    public void exportZpspList(HttpServletResponse response, ZhsqQueryCriteria criteria) throws IOException {

        List<String> list = new ArrayList<>();
        list.add(DefaultUtil.ZHZT_DSP);
        criteria.setBisZhsqZt(list);

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setBisPggsId(gsgl.getBisGsglId());

        zhsqService.downloadComplete(zhsqService.queryAll(criteria), response);
    }

    @ApiOperation("自评驳回列表")
    @GetMapping(value = "/queryZpbhList")
    public ResponseEntity<Object> queryZpbhList(HomeQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        return new ResponseEntity<>(zhsqService.queryZpbhList(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), gsgl.getBisGsglId(), pageable), HttpStatus.OK);
    }

    @Log("导出自评驳回")
    @ApiOperation("导出自评驳回")
    @GetMapping(value = "/queryZpbhList/download")
    public void exportZpbhList(HttpServletResponse response, ZhsqQueryCriteria criteria) throws IOException {

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);

        zhsqService.downloadZpbhList(zhsqService.queryZpbhList(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), gsgl.getBisGsglId()), response);
    }

    @GetMapping(value = "/jjspList")
    @ApiOperation("查询所有的居家申请")
    public ResponseEntity<Object> jjspList(ZhsqQueryCriteria criteria, Pageable pageable){
        criteria.setBisZhsqZhlb("03");
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/querySelfAssess")
    @ApiOperation("查询已评估的照护")
    public ResponseEntity<Object> querySelfAssess(@RequestParam(defaultValue = "") String name,
                                                  @RequestParam(defaultValue = "0")Integer page,
                                                  @RequestParam(defaultValue = "10")Integer size){
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        System.out.println(userJson);
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        System.out.println(roleString);
        if(roleString.indexOf("商保公司") != -1) {
            Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
            return new ResponseEntity<>(zhsqService.querySelfAssess(roleString, gsgl.getBisGsglId(), startEnd),HttpStatus.OK);
        } else {
            return new ResponseEntity<>(zhsqService.querySelfAssess(roleString, String.valueOf(deptJson.get("id")), startEnd),HttpStatus.OK);
        }
    }

    @GetMapping(value = "/completeHome")
    @ApiOperation("查询审批通过的照护")
    public ResponseEntity<Object> queryCompleteHome(ZhsqQueryCriteria criteria, Pageable pageable){
        List<String> list = new ArrayList<String>();
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("12");
        list.add("13");
        list.add("90");
        criteria.setBisZhsqZt(list);

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisSbgsId(gsgl.getBisGsglId());
        criteria.setBisZhsqZhlb("03");
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/completeOrg")
    @ApiOperation("查询审批通过的照护")
    public ResponseEntity<Object> queryCompleteOrg(ZhsqQueryCriteria criteria, Pageable pageable){
        List<String> list = new ArrayList<String>();
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("12");
        list.add("13");
        list.add("90");
        criteria.setBisZhsqZt(list);

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisSbgsId(gsgl.getBisGsglId());
        criteria.setBisZhsqZhlb("01");
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/completeJbzx")
    @ApiOperation("经办中心查询审批通过的照护")
    public ResponseEntity<Object> queryCompleteJbzx(ZhsqQueryCriteria criteria, Pageable pageable) {
        List<String> list = new ArrayList<String>();
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("12");
        list.add("13");
        list.add("90");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/complete/download")
    public void exportWhfa(HttpServletResponse response, ZhsqQueryCriteria criteria) throws IOException {
        List<String> list = new ArrayList<String>();
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("12");
        list.add("13");
        list.add("90");
        criteria.setBisZhsqZt(list);

        UserDetails userDetails = SecurityUtils.getCurrentUser();
        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
            criteria.setBisSbgsId(gsgl.getBisGsglId());
        } else {
            criteria.setBisZhsqSnryYbssqx(String.valueOf(deptJson.get("id")));
        }
        zhsqService.downloadComplete(zhsqService.queryAll(criteria), response);
    }

    @GetMapping(value = "/findZhsqById")
    @ApiOperation("根据主键查询照护申请信息")
    public ResponseEntity<Object> findZhsqById(@RequestParam Long zhid){
        ZhsqDto zhsqDto = zhsqService.findById(zhid);
        return new ResponseEntity<>(zhsqDto, HttpStatus.OK);
    }

    @PostMapping
    @Log("机构新增照护申请")
    @ApiOperation("机构新增照护申请")
    @PreAuthorize("@el.check('zhsq:add')")
    public ResponseEntity<Object> createZhsq(@Validated @RequestBody Zhsq resources, @RequestParam String ids){

        return new ResponseEntity<>(zhsqService.create(resources, ids),HttpStatus.CREATED);
    }

    @PostMapping(value = "/createGl")
    @Log("机构新增照护高龄申请")
    @ApiOperation("机构新增照护高龄申请")
    @PreAuthorize("@el.check('zhsq:add')")
    public ResponseEntity<Object> createGl(@Validated @RequestBody Zhsq resources, @RequestParam String ids, @RequestParam String delay){

        return new ResponseEntity<>(zhsqGlService.createGl(resources, ids, delay),HttpStatus.CREATED);
    }

    @PutMapping(value = "/updateGl")
    @Log("修改照护高龄申请")
    @ApiOperation("修改照护高龄申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> updateGl(@Validated @RequestBody Zhsq resources, @RequestParam String ids){
        return new ResponseEntity<>(zhsqGlService.updateGl(resources, ids), HttpStatus.OK);
    }

    @PutMapping(value = "/submitGl")
    @Log("提交照护高龄申请")
    @ApiOperation("提交照护高龄申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> submitGl(@Validated @RequestBody Zhsq resources, @RequestParam String ids, @RequestParam String delay){
        return new ResponseEntity<>(zhsqGlService.submitGl(resources, ids, delay), HttpStatus.OK);
    }

    @PostMapping(value = "/createSn")
    @Log("机构新增照护失能申请")
    @ApiOperation("机构新增照护失能申请")
    @PreAuthorize("@el.check('zhsq:add')")
    public ResponseEntity<Object> createSn(@Validated @RequestBody Zhsq resources, @RequestParam String ids, @RequestParam String delay){

        return new ResponseEntity<>(zhsqSnService.createSn(resources, ids, delay),HttpStatus.CREATED);
    }

    @PutMapping(value = "/updateSn")
    @Log("修改照护失能申请")
    @ApiOperation("修改照护失能申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> updateSn(@Validated @RequestBody Zhsq resources, @RequestParam String ids){
        return new ResponseEntity<>(zhsqSnService.updateSn(resources, ids), HttpStatus.OK);
    }

    @PutMapping(value = "/submitSn")
    @Log("提交照护失能申请")
    @ApiOperation("提交照护失能申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> submitSn(@Validated @RequestBody Zhsq resources, @RequestParam String ids, @RequestParam String delay){
        return new ResponseEntity<>(zhsqSnService.submitSn(resources, ids, delay), HttpStatus.OK);
    }

    @PutMapping(value = "/updateOnlyZhsq")
    @Log("修改照护申请")
    @ApiOperation("修改照护申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> updateOnlyZhsq(@Validated @RequestBody Zhsq resources){
        return new ResponseEntity<>(zhsqService.update(resources), HttpStatus.OK);
    }

    @PutMapping
    @Log("修改居家照护申请")
    @ApiOperation("修改居家照护申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> updateZhsq(@Validated @RequestBody Zhsq resources, @RequestParam String ids){
        return new ResponseEntity<>(zhsqService.update(resources, ids), HttpStatus.OK);
    }

    @PutMapping(value = "/onlyEdit")
    @Log("修改居家照护申请")
    @ApiOperation("修改居家照护申请")
    public ResponseEntity<Object> updateZhsq(@Validated @RequestBody Zhsq resources){
        zhsqService.update(DateUtil.format(resources.getBisZhsqTjsj(), DatePattern.NORM_DATE_PATTERN), resources.getBisZhsqId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "/submitZhsq")
    @Log("机构提交照护申请")
    @ApiOperation("机构提交照护申请")
    @PreAuthorize("@el.check('zhsq:edit')")
    public ResponseEntity<Object> submitZhsq(@Validated @RequestBody Zhsq resources, @RequestParam String ids){
        return new ResponseEntity<>(zhsqService.submitZhsq(resources, ids), HttpStatus.OK);
    }

    @PutMapping(value = "/checkZhsq")
    @Log("PC端复核照护申请")
    @ApiOperation("PC端复核照护申请")
    public ResponseEntity<Object> checkZhsq(@Validated @RequestBody Zhsq resources) {
        zhsqService.updateFhZhzt(resources.getBisZhsqZt(), resources.getBisZhsqId(), resources.getBz());
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/checkPgPcjgzh")
    @Log("经办中心PC端评估照护申请")
    @ApiOperation("经办中心PC端评估照护申请")
    public ResponseEntity<Object> checkPgPcjgzh(@Validated @RequestBody Zhsq resources) {

        Long zhid = resources.getBisZhsqId();
        String zt = resources.getBisZhsqZt();
        zhsqService.checkPgPcjgzh(zt, zhid, resources.getBz(), resources);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/checkJbzxZhsq")
    @Log("经办中心PC端复核照护申请")
    @ApiOperation("经办中心PC端复核照护申请")
    public ResponseEntity<Object> checkJbzxZhsq(@Validated @RequestBody Zhsq resources) {

        Long zhid = resources.getBisZhsqId();
        String zt = resources.getBisZhsqZt();
        zhsqService.checkJbzxZhsq(zt, zhid, resources.getBz(), resources);
        if(!DebugProperties.debug) {
            log.info("生产模式");
            if(zt.equals("05")) { // 复核通过
                log.info("[ZhsqController - checkJbzxZhsq]--------------- 经办中心PC端复核照护申请 - 执行2901");
                zhsqProService.make2901(SecurityUtils.getCurrentUserId(), zhid);
            }
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * PC端，自评审批和审核用到
     * @param resources
     * @return
     */
    @PutMapping(value = "/zpspZhsq")
    @Log("PC端自评审批")
    @ApiOperation("PC端自评审批")
    public ResponseEntity<Object> zpspZhsq(@RequestBody Zhsq resources) {

        Long userid = SecurityUtils.getCurrentUserId();
        Long zhid = resources.getBisZhsqId();
        zhsqService.updatePcZhzt(resources.getBisZhsqZt(), resources.getBisZhsqId(), resources.getBz());
        /*
        2023年新中标的商保公司没有复核费用。add by xiahongbo on 2023-03-07
        if(!DebugProperties.debug) {
            log.info("生产模式");
            if(resources.getBisZhsqZt().equals(DefaultUtil.ZHZT_DFP)) { // 自评审批通过
                pgfyProService.jzdj2210(userid, zhid);
                pgfyProService.fymx2310(userid, zhid);
                pgfyProService.fyjs2410(userid, zhid);
            }
        } else {
            log.info("开发模式");
        }
        */
        return new ResponseEntity<>(PageUtil.toMpContent(new HashMap<String,Object>()),HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除居家照护申请")
    @ApiOperation("删除居家照护申请")
    @PreAuthorize("@el.check('zhsq:del')")
    public ResponseEntity<Object> deleteZhsq(@RequestBody Long[] ids) {
        zhsqService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping(value = "/cancelZhsq")
    @Log("作废照护申请")
    @ApiOperation("作废照护申请")
    public ResponseEntity<Object> cancelZhsq(@RequestParam Long zhid, @RequestParam String cause) {
        zhsqService.cancelZhsq(cause, zhid);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/queryZhid")
    public ResponseEntity<Object> queryZhid(@RequestParam String sfz) {
        //zhsqService.update(resources);
        //return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        //bisLogService.save(resources.getBisZhsqId(), "提交居家照护申请", "me.zhengjie.business.rest.ZhsqController.submitZhsq()", request);

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        return new ResponseEntity<>(zhsqService.queryZhid(sfz, gsgl.getBisGsglYljgdm()), HttpStatus.OK);
    }

    @GetMapping(value = "/queryHomeWapprove")
    @ApiOperation("查询未分配的居家照护")
    public ResponseEntity<Object> queryHomeWapprove(ZhsqQueryCriteria criteria, Pageable pageable){

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisPggsId(gsgl.getBisGsglId());
        criteria.setBisZhsqZhlb("03");

        List<String> list = new ArrayList<>();
        list.add("01");
        criteria.setBisZhsqZt(list);


        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ApiOperation("导出未分配的居家照护")
    @GetMapping(value = "/queryHomeWapprove/download")
    public void exportHomeWapprove(HttpServletResponse response, ZhsqQueryCriteria criteria) throws IOException {

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisPggsId(gsgl.getBisGsglId());
        criteria.setBisZhsqZhlb("03");

        List<String> list = new ArrayList<>();
        list.add("01");
        criteria.setBisZhsqZt(list);

        zhsqService.downloadHomeApprove(zhsqService.queryAll(criteria), response);
    }


    @GetMapping(value = "/queryHomeYapprove")
    @ApiOperation("查询已分配的机构照护")
    public ResponseEntity<Object> queryHomeYapprove(ZhsqQueryCriteria criteria, Pageable pageable){

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisPggsId(gsgl.getBisGsglId());
        criteria.setBisZhsqZhlb("03");

        List<String> list = new ArrayList<String>();
        list.add("02");
        list.add("03");
        criteria.setBisZhsqZt(list);
        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ApiOperation("导出已分配的居家照护")
    @GetMapping(value = "/queryHomeYapprove/download")
    public void exportHomeYapprove(HttpServletResponse response, ZhsqQueryCriteria criteria) throws IOException {

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisPggsId(gsgl.getBisGsglId());
        criteria.setBisZhsqZhlb("03");

        List<String> list = new ArrayList<>();
        list.add("02");
        list.add("03");
        criteria.setBisZhsqZt(list);

        zhsqService.downloadHomeApprove(zhsqService.queryAll(criteria), response);
    }

    @GetMapping(value = "/updateById")
    @ApiOperation("根据订单id更改相机状态")
    public ResponseEntity<Object> updateById(@RequestParam String orderId){
        return new ResponseEntity<>(zhsqService.updateById(orderId),HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllQyJd")
    @ApiOperation("移动端查询所有区域街道")
    public ResponseEntity<Object> queryAllQyJd(SnryQueryCriteria criteria) {
        com.alibaba.fastjson.JSONArray objects = mpSnryService.queryQyJd();
        return new ResponseEntity<>(objects, HttpStatus.OK);
    }

    @PostMapping(value = "/getSelfAnalyse")
    @ApiOperation("移动端查询所有区域街道")
    public ResponseEntity<Object> getSelfAnalyse(@RequestBody Zhsq resources) {

        String selfSndj = "";
        int a0 = 0;
        int a2 = 0;
        int b0 = 0;
        int b2 = 0;
        for (AdlZhsq adlZhsq: resources.getAdlZhsqs()) {

            String adlXmId = adlZhsq.getAdlXmId();
            String fs = adlZhsq.getAdlXmxxFs();
            if("402883929517f0cd0195180c163e0001".equals(adlXmId) || "402883929517f0cd0195180c70940003".equals(adlXmId) || "402883929517f0cd0195180cae4a0005".equals(adlXmId)) {

                if("0".equals(fs)) {
                    a0++;
                }
                if("2".equals(fs)) {
                    a2++;
                }

            }

            if("402883929517f0cd0195180c2f0a0002".equals(adlXmId) || "402883929517f0cd0195180c8d990004".equals(adlXmId) || "402883929517f0cd0195180d367a0006".equals(adlXmId)) {

                if("0".equals(fs)) {
                    b0++;
                }
                if("2".equals(fs)) {
                    b2++;
                }
            }
        }

        System.out.println("a2 ==3 && b2 ==3"+(a2 ==3 && b2 ==3));
        System.out.println("a0 == 1 || b0 == 1 || b0 == 2"+(a0 == 1 || b0 == 1 || b0 == 2));
        System.out.println("(a0 == 1 && b0 == 1) || b0 == 3"+((a0 == 1 && b0 == 1) || b0 == 3));
        System.out.println("a0 ==2 || (a0 ==1 && b0 ==2)"+(a0 ==2 || (a0 ==1 && b0 ==2)));
        System.out.println("(a0 ==3 || (a0 == 2 && (b0 == 1 || b0 ==2)) || (a0 ==1 && b0 == 3))"+(a0 ==3 || (a0 == 2 && (b0 == 1 || b0 ==2)) || (a0 ==1 && b0 == 3)));
        System.out.println("((a0 == 3 && (b0 == 1 || b0 ==2)) || (a0 ==2 && b0 == 3))"+((a0 == 3 && (b0 == 1 || b0 ==2)) || (a0 ==2 && b0 == 3)));
        System.out.println("(a0 == 3 && b0 == 3)"+(a0 == 3 && b0 == 3));
        if(a2 ==3 && b2 ==3) {
            selfSndj = "A级";
        }   if(a0 == 1 || b0 == 1 || b0 == 2) {

            selfSndj = "B级";
        }   if((a0 == 1 && b0 == 1) || b0 == 3) {

            selfSndj = "C级";
        }   if(a0 ==2 || (a0 ==1 && b0 ==2)) {

            selfSndj = "D级";
        }   if(a0 ==3 || (a0 == 2 && (b0 == 1 || b0 ==2)) || (a0 ==1 && b0 == 3)) {

            selfSndj = "E级";
        }   if((a0 == 3 && (b0 == 1 || b0 ==2)) || (a0 ==2 && b0 == 3)) {

            selfSndj = "F级";
        }   if(a0 == 3 && b0 == 3) {

            selfSndj = "G级";
        }
        return new ResponseEntity<>(selfSndj, HttpStatus.OK);
    }

    @GetMapping(value = "/calculateOrgDelayDays")
    @ApiOperation("计算机构延期天数")
    public ResponseEntity<Object> calculateOrgDelayDays(@RequestParam Long zhid) {
        int days = zhsqService.calculateOrgDelayDays(zhid);
        return new ResponseEntity<>(days, HttpStatus.OK);
    }

    @GetMapping(value = "/getFpTargetDate")
    @ApiOperation("获取评估日期")
    public ResponseEntity<Object> getFpTargetDate(@RequestParam Long zhid) {
        String days = zhsqService.getFpTargetDate(zhid);
        if (StringUtils.isEmpty(days)) {
            return new ResponseEntity<>(days, HttpStatus.OK);
        } else {
            days = DateTimeUtil.getDateTime(DateTimeUtil.addDays(DateTimeUtil.parse(days, DateTimeUtil.DEFAULT_DATE_FORMAT), 5), "yyyy年MM月dd日");
            return new ResponseEntity<>(days, HttpStatus.OK);
        }
    }



    @GetMapping(value = "/pdfScore")
    public ResponseEntity<Object> getPdfScore(String zhsqId) {
        List<Map<String, Object>> map = zhsqService.getPdfScore(zhsqId);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

}
